#include "PageRememberSettings.h"


using namespace MSXML2;

CPageRememberSettings::CPageRememberSettings(void)
{
	
}

CPageRememberSettings::CPageRememberSettings(CPageRememberSettings& settings)
{
	
}

void CPageRememberSettings::operator=(CPageRememberSettings& settings)
{
	
}

CPageRememberSettings::~CPageRememberSettings(void)
{
	
}

long CPageRememberSettings::GetScrollPercentage(CString strDocumentName)
{
	long percentage = 0;
	int index = m_mapRemember.FindKey(strDocumentName);
	if(-1!=index)
	{
		percentage = m_mapRemember.GetValueAt(index);
	}
	return percentage;
}

void CPageRememberSettings::Load()
{
	MSXML2::IXMLDOMDocument *iXMLDoc = NULL;
	HRESULT hr = CoCreateInstance (MSXML2::CLSID_DOMDocument, NULL, 
                 CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
                 MSXML2::IID_IXMLDOMDocument,(LPVOID *)&iXMLDoc);
	if(SUCCEEDED(hr))
	{
		CString settingsPath;
		if(TRUE == GetSettingsFilePath(settingsPath))
		{
			VARIANT_BOOL bSuccess;
			hr = iXMLDoc->load(CComVariant(settingsPath), &bSuccess);
			if(SUCCEEDED(hr))
			{
				MSXML2::IXMLDOMNodeList* pDocumentNodeList = NULL;
				hr = iXMLDoc->getElementsByTagName(L"Document", &pDocumentNodeList);
				if(SUCCEEDED(hr))
				{
					long lCount = 0;					
					hr = pDocumentNodeList->get_length(&lCount);
					if(SUCCEEDED(hr))
					{
						for(long lIndex = 0; lIndex < lCount; lIndex++)
						{
							MSXML2::IXMLDOMNode* pDocumentNode = NULL;
							hr = pDocumentNodeList->get_item(lIndex, &pDocumentNode);
							if(SUCCEEDED(hr))
							{
								MSXML2::IXMLDOMNodeList* pItemsList = NULL;
								hr = pDocumentNode->get_childNodes(&pItemsList);
								if(SUCCEEDED(hr))
								{
									MSXML2::IXMLDOMNode* pPathNode = NULL;
									pItemsList->get_item(0, &pPathNode);
									BSTR nodeText;
									pPathNode->get_text(&nodeText);
									CString strPath(nodeText);
									MSXML2::IXMLDOMNode* pPercentageNode = NULL;
									pItemsList->get_item(1, &pPercentageNode);
									pPercentageNode->get_text(&nodeText);
									CString strPercentage(nodeText);
									Add(strPath, _wtol(nodeText));
								}
							}
						}
					}
				}
			}
		}
	}
}

void CPageRememberSettings::Save()
{
	CString settingsPath;
	if(TRUE == GetSettingsFilePath(settingsPath))
	{
		MSXML2::IXMLDOMDocument *iXMLDoc = NULL;
		HRESULT hr = CoCreateInstance (MSXML2::CLSID_DOMDocument, NULL, 
                 CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
                 MSXML2::IID_IXMLDOMDocument,(LPVOID *)&iXMLDoc);

		if(SUCCEEDED(hr))
		{
			MSXML2::IXMLDOMElement* idocumentsElement = NULL;
			hr = iXMLDoc->createElement(L"Documents",  &idocumentsElement);
			if(SUCCEEDED(hr))
			{
				MSXML2::IXMLDOMNode* newNode = NULL;
				hr = iXMLDoc->appendChild(idocumentsElement, &newNode);
				if(SUCCEEDED(hr))
				{
					int size = m_mapRemember.GetSize();
					for(int index = 0; index < size; index++)
					{
						MSXML2::IXMLDOMElement* idocumentElement = NULL;
						MSXML2::IXMLDOMNode* idocumentNode = NULL;
						MSXML2::IXMLDOMNode* iPathNode = NULL;
						iXMLDoc->createElement(L"Document", &idocumentElement);
						newNode->appendChild(idocumentElement, &idocumentNode);
						CString strKey = m_mapRemember.GetKeyAt(index);
						long lPercentageScrolled = m_mapRemember.GetValueAt(index);
						MSXML2::IXMLDOMElement* iPathElement = NULL;
						iXMLDoc->createElement(L"Path", &iPathElement);
						iPathElement->put_text(strKey.AllocSysString());
						idocumentNode->appendChild(iPathElement, &iPathNode);

						MSXML2::IXMLDOMElement* iPercentScrollElement = NULL;
						iXMLDoc->createElement(L"PercentageScrolled", &iPercentScrollElement);
						CString strPercentageScrolled;
						strPercentageScrolled.Format(_T("%ld"), lPercentageScrolled);
						iPercentScrollElement->put_text(strPercentageScrolled.AllocSysString());
						idocumentNode->appendChild(iPercentScrollElement, &iPathNode);
					}
				}
			}
			VARIANT filePath;
			filePath.vt = VT_BSTR;
			filePath.bstrVal = settingsPath.AllocSysString();
			iXMLDoc->save(filePath);
		}
	}
}

void CPageRememberSettings::Add(CString strPath, long percentageScrolled)
{
	int index = m_mapRemember.FindKey(strPath);
	if(-1 == index)//new entry
	{
		m_mapRemember.Add(strPath, percentageScrolled);
		if(m_mapRemember.GetSize() > DEFAULT_REMEMBER_SIZ)
		{
			while(m_mapRemember.GetSize() == DEFAULT_REMEMBER_SIZ)
			{
				m_mapRemember.RemoveAt(0);
			}
		}
	}
	else//already exists.
	{
		m_mapRemember.SetAt(strPath, percentageScrolled);
	}
}

BOOL CPageRememberSettings::GetSettingsFilePath(CString& path)
{
	BOOL bRet = FALSE;
	CHAR my_documents[MAX_PATH];
    HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);
	if(SUCCEEDED(result))
	{
		CString strPath = my_documents;
		path.Format(_T("%s\\%s"), strPath, DEFAULT_SETTINGS_FILENAME);
		bRet = TRUE;
	}
	return bRet;
}
